import Vue from 'vue'
import vuex from 'vuex'
import axios from 'axios'
import {nanoid} from 'nanoid'

Vue.use(vuex)

//定义一个action，里面放响应组件动作的函数（服务员）
const actions = {
	async addNews({commit}){
		let result = await axios.get('https://api.uixsj.cn/hitokoto/get?type=social')
		const newsObj = {id:nanoid(),title:result.data}
		commit('ADD_NEWS',newsObj)
	}
}

//定义一个mutations，里面放操作数据的函数（厨师）
const mutations = {
	ADD_PERSON(state,value){
		state.persons.unshift(value)
	},
	ADD_NEWS(state,value){
		state.newsList.unshift(value)
	}
}

//定义一个state，初始化数据（原材料）
const state = {
	school:{
		name:'尚硅谷',
		address:'宏福科技园'
	},
	persons:[
		{id:'hy7atsd7t76',name:'强哥',age:18},
		{id:'hy7atsd7y89',name:'沫沫',age:16}
	],
	newsList:[
		{id:'sau32d382',title:'震惊，两男子居然做出这种事！'},
		{id:'sau56d393',title:'这10种食物注意了，致癌！'}
	]
}

//创建一个store，并管理actions、mutation、state
const store = new vuex.Store({
	actions,
	mutations,
	state
})

//暴露
export default store